package com.onstar.cn.framework.retrofit;

/**
 * Created by 任任 on 2017/3/19.
 */

public class BaseApi {



    public final static String HTTPS = "https";
    public final static String BASE_URL = "https://api-idt5.shanghaionstar.com:7443";
    public final static String HEADER_ACCEPT = "Accept";
    public final static String HEADER_AUTHORIZATION = "Authorization";
    public final static String HEADER_CONTENT_TYPE = "Content-Type";
    public final static String HEADER_ACCEPT_LANGUAGE = "Accept-Language";
    public final static String HEADER_CLIENT_INFO = "CLIENT-INFO";
    public final static String HEADER_CLIENT_TRACE_ID = "CLIENT-TRACE-ID";

    public interface ISuffixUrl {
        String MSP_PROFILE = "/msp/api/v3/user/profile";
        String OAUTH_TOKEN = "/api/v1/oauth/token";
        String VEHICLE_COMMANDS = "/api/v1/account/vehicles/%s/commands";
    }


//    public void addSslFactory(OkHttpClient requestParams) {
//        String url = requestParams.getUri();
//        assert url != null;
//        if (url.contains(HTTPS)) {
//            try {
//                SSLContext sslcontext = SSLContext.getInstance("TLSv1");
//                sslcontext.init(null, null, null);
//                SSLSocketFactory mSslFactory = new NoSSLv3SocketFactory(sslcontext.getSocketFactory());
//                requestParams.sslSocketFactory(mSslFactory);
//            } catch (NoSuchAlgorithmException e1) {
//                e1.printStackTrace();
//            } catch (KeyManagementException e1) {
//                e1.printStackTrace();
//            }
//        }
//    }

//    public void addCommonHeader(OkHttpClient  requestParams, String authorization) {
//        requestParams.newBuilder(HEADER_ACCEPT, "application/json");
//        requestParams.setHeader(HEADER_CONTENT_TYPE, "application/json" + ";charset=utf-8");
//        requestParams.setHeader(HEADER_ACCEPT_LANGUAGE, "zh-CN");
//        addClientInfo(requestParams);
//        addAuthorization(requestParams, authorization);
//        requestParams.setHeader(HEADER_CLIENT_TRACE_ID, ClientTraceInstance.getInstance().getClientTraceId());
//    }
//
//    private void addAuthorization(RequestParams requestParams, String authorization) {
//        String url = requestParams.getUri();
//        if (!CommonUtil.isNull(url) && (url.contains("mag") || url.contains("msp"))) {
//            requestParams.setHeader(HEADER_AUTHORIZATION, TextUtils.isEmpty(authorization) ? HEADER_AUTHORIZATION : authorization);
//        } else {
//            if (!TextUtils.isEmpty(authorization))
//                requestParams.setHeader(HEADER_AUTHORIZATION, authorization);
//        }
//    }
//
//    private void addClientInfo(RequestParams requestParams) {
//        StringBuilder stringBuilder = new StringBuilder();
//        stringBuilder.append("android");
//        stringBuilder.append("_");
//        stringBuilder.append("7.1");
//        stringBuilder.append("_");
//        if (DeviceUtil.getLanguage() != null && DeviceUtil.getCountry() != null) {
//            stringBuilder.append(DeviceUtil.getLanguage() + "-" + DeviceUtil.getCountry().toUpperCase());
//        } else {
//            stringBuilder.append("zh-CN");
//        }
//        stringBuilder.append("_");
//        stringBuilder.append(DeviceUtil.getDeviceType());
//        requestParams.setHeader(HEADER_CLIENT_INFO, stringBuilder.toString());
//    }
//
////    "https://api-idt5.shanghaionstar.com:7443/api/v1/oauth/token"
//
//    public <T> void post(String url, String authorization, Callback.CommonCallback<T> commonCallback, BaseRequestBean bean) {
//        RequestParams requestParams = getRequestParams(url, authorization, bean);
//        x.http().post(requestParams, commonCallback);
//    }
//
//
//    @NonNull
//    private RequestParams getRequestParams(String url, String authorization, BaseRequestBean bean) {
//        RequestParams requestParams = getRequestParams(url, authorization);
//        String requestBody = new Gson().toJson(bean);
//        requestParams.setBodyContent(requestBody);
//        return requestParams;
//    }
//
//    @NonNull
//    private RequestParams getRequestParams(String url, String authorization, String requestBody) {
//        RequestParams requestParams = getRequestParams(url, authorization);
//        requestParams.setBodyContent(requestBody);
//        return requestParams;
//    }
//
//    @NonNull
//    private RequestParams getRequestParams(String url, String authorization) {
//        String fullUrl = BASE_URL + url;
//        RequestParams requestParams = new RequestParams(fullUrl);
//        addSslFactory(requestParams);
//        addCommonHeader(requestParams, authorization);
//        return requestParams;
//    }
//
//    public String getHeaderAuthorization() {
//        OnstarApplication application = OnstarApplication.getApplication();
//        AuthTokenEntity entity = application.getmAuthTokenEntity();
//        if (!CommonUtil.isNull(entity))
//            return entity.getTokenType() + " " + entity.getAccessToken();
//        return "";
//    }
//
//    public <T> void post(String url, BaseRequestBean bean, Callback.CommonCallback<T> commonCallback) {
//        RequestParams requestParams = getRequestParams(url, getHeaderAuthorization(), bean);
//        x.http().post(requestParams, commonCallback);
//    }
//
//    public <T> void post(String url, String requestBody, Callback.CommonCallback<T> commonCallback) {
//        RequestParams requestParams = getRequestParams(url, getHeaderAuthorization(), requestBody);
//        x.http().post(requestParams, commonCallback);
//    }
//
//    public <T> void get(String url, Callback.CommonCallback<T> commonCallback) {
//        RequestParams requestParams = getRequestParams(url, getHeaderAuthorization());
//        x.http().get(requestParams, commonCallback);
//    }
//
//    public <T> void put(String url, BaseRequestBean bean, Callback.CommonCallback<T> commonCallback) {
//        RequestParams requestParams = getRequestParams(url, getHeaderAuthorization(), bean);
//        x.http().request(HttpMethod.PUT, requestParams, commonCallback);
//    }

}
